/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Test;

import RPS.EquivalenceMapping;
import RPS.MappingAssertion;
import RPS.PeerSchema;
import RPS.RPS;
import Rewriting.FederationManager;
import Rewriting.Source;
import SPARQL.SimpleURI;
import parser.ParseException;
import parser.SparqlParser;

/**
 *
 * @author mirko
 */
public class TestFactory {
    
    private Source source1;
    private Source source2;
    private Source source3;
    
    public TestFactory(){
        
        source1 = new Source();
        source2 = new Source();
        source3 = new Source();
        
        // Definition SOURCE 1 DBpedia
        source1.setEndpoint(new SimpleURI("http://dbpedia.org/sparql"));
        PeerSchema schema1 = new PeerSchema();
        schema1.addConstant(new SimpleURI("http://uk.dbpedia.org/resource/Mulholland_Drive"));
        schema1.addConstant(new SimpleURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
        schema1.addConstant(new SimpleURI("http://dbpedia.org/resource/Mulholland_Drive_(film)"));
        schema1.addConstant(new SimpleURI("http://dbpedia.org/ontology/starring"));
        schema1.addConstant(new SimpleURI("http://dbpedia.org/ontology/Film"));
        schema1.addConstant(new SimpleURI("http://dbpedia.org/ontology/birthDate"));
        
        
        
        source1.setSchema(schema1);
        
        // Definition SOURCE 2 LinkedMDB
        source2.setEndpoint(new SimpleURI("http://data.linkedmdb.org/sparql"));
        PeerSchema schema2 = new PeerSchema();
        schema2.addConstant(new SimpleURI("http://data.linkedmdb.org/resource/film/21"));
        schema2.addConstant(new SimpleURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
        schema2.addConstant(new SimpleURI("http://data.linkedmdb.org/resource/movie/actor"));
        schema2.addConstant(new SimpleURI("http://data.linkedmdb.org/resource/movie/film"));
        
        source2.setSchema(schema2);
        
        // Definition SOURCE 3 FactForge
        source3.setEndpoint(new SimpleURI("http://factforge.net/sparql"));
        PeerSchema schema3 = new PeerSchema();
        schema3.addConstant(new SimpleURI("http://rdf.freebase.com/ns/film.film.starring"));
        schema3.addConstant(new SimpleURI("http://rdf.freebase.com/ns/film.film"));
        
        schema3.addConstant(new SimpleURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"));
        schema3.addConstant(new SimpleURI("http://dbpedia.org/resource/Mulholland_Drive_(film)")); 
        
        source3.setSchema(schema3);
    }

    RPS createRPS() throws ParseException {
        RPS peersytem = new RPS();
        peersytem.addPeerSchema(source1.getSchema());
        peersytem.addPeerSchema(source2.getSchema());
        peersytem.addPeerSchema(source3.getSchema());
        
        
        //Define equivalence mappings
        //ripristinare in caso viene tolto l'equality mapping sotto
        peersytem.addEMapping(newEquimapping("http://data.linkedmdb.org/resource/film/21", "http://uk.dbpedia.org/resource/Mulholland_Drive"));
        peersytem.addEMapping(newEquimapping("http://dbpedia.org/resource/Mulholland_Drive_(film)", "http://uk.dbpedia.org/resource/Mulholland_Drive"));
        
        //equality mappings for query 4 
        //peersytem.addEMapping(newEquimapping("http://dbpedia.org/resource/Mulholland_Drive_(film)", "http://data.linkedmdb.org/resource/film/21"));
       
        
//Define graph mapping assertions
       


        //predicate mappings
        peersytem.addGMapping(newPredmapping( "http://rdf.freebase.com/ns/film.film.starring", "http://dbpedia.org/ontology/starring"));
        peersytem.addGMapping(newPredmapping( "http://dbpedia.org/ontology/starring", "http://rdf.freebase.com/ns/film.film.starring"));
        peersytem.addGMapping(newPredmapping( "http://data.linkedmdb.org/resource/movie/actor", "http://dbpedia.org/ontology/starring"));
        peersytem.addGMapping(newPredmapping( "http://dbpedia.org/ontology/starring", "http://data.linkedmdb.org/resource/movie/actor" ));
        //class mappings
        peersytem.addGMapping(newClassmapping( "http://dbpedia.org/ontology/Film", "http://data.linkedmdb.org/resource/movie/film"));
        peersytem.addGMapping(newClassmapping( "http://data.linkedmdb.org/resource/movie/film", "http://rdf.freebase.com/ns/film.film"));
        
        
        
        return peersytem;
    }
    
    private EquivalenceMapping newEquimapping(String string1, String string2) throws ParseException{
        return new EquivalenceMapping(new SimpleURI(string1),
        new SimpleURI(string2));
    }
    
    private MappingAssertion newPredmapping(String string1, String string2) throws ParseException {
        
        return new MappingAssertion(SparqlParser.parse("SELECT ?x ?y WHERE {?x <" + string1+"> ?y}"),
        SparqlParser.parse("SELECT ?x ?y WHERE {?x <"+ string2+ "> ?y}"));
        
    }
    private MappingAssertion newClassmapping(String string1, String string2) throws ParseException{
        return new MappingAssertion(SparqlParser.parse("SELECT ?x WHERE {?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <"+ string1 + ">}"),
        SparqlParser.parse("SELECT ?x WHERE {?x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" + string2+ ">}"));
        
    }
    
    FederationManager createFederationManager() {
        
        FederationManager fedmanager = new FederationManager();
        
       
        
        //Adding the sources to the manager
        fedmanager.addSource(source1);
        fedmanager.addSource(source2);
        fedmanager.addSource(source3);
        return fedmanager;
    }
}
